<?php
/**
 * 描述
 * Created by Paco
 * Date: 2018/11/16 Time: 14:41
 */

namespace App\Repositories\Product;


use App\Models\Product\Brand;
use App\Repositories\Repository;

class BrandRepository extends Repository
{
    public function brand()
    {
        $brands = Brand::query()->paginate($this->page_size, ['*'], 'page', $this->page);
        return $this->respondWith($brands);
    }

    public function show($brand_id)
    {
        $brand = Brand::query()->find($brand_id);
        return $this->respondWith($brand);
    }

    public function store($data)
    {
        try {
            Brand::query()->create($data);
            return $this->respondSuccess();
        } catch (\Exception $e) {
            $this->throwRespondError(10110);
        }
    }

    public function save($params)
    {
        //$params = array_filter($params, 'my_array_filter');
        $brand = Brand::query()->find($params['brand_id']);
        if (!$brand) {
            $this->throwRespondError(10109);
        }
        try {
            $brand->fill($params)->save();
            return $this->respondSuccess();
        } catch (\Exception $e) {
            $this->throwRespondError(10108);
        }
    }


    public function del($brand_ids)
    {
        if (!is_array($brand_ids)) {
            $brand_ids = explode(',', $brand_ids);
        }
        try {
            Brand::query()->whereIn('brand_id', $brand_ids)->delete();
            return $this->respondSuccess();
        } catch (\Exception $e) {
            $this->throwRespondError(10107);
        }
    }
}